#include "TextureDrawer.h"

TextureDrawer::TextureDrawer(std::vector<Image*> i, std::vector<GLuint> t, int count){
	images = i;
	textureIds = t;
	numOfTextures = count;
}

TextureDrawer::~TextureDrawer(){
}

void TextureDrawer::enable2DTexturing(int id){
	glEnable(GL_TEXTURE_2D);	
	if(id < numOfTextures){
		glBindTexture(GL_TEXTURE_2D, textureIds[id]);
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glTexSubImage2D(GL_TEXTURE_2D, 4, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, images[id]->pixels);
	}
}

void TextureDrawer::disable2DTexturing(){
	glDisable(GL_TEXTURE_GEN_T);
	glDisable(GL_TEXTURE_GEN_S);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
	glDisable(GL_TEXTURE_2D);
}

// Draw a textured cube
void TextureDrawer::drawTexturedCube(int id, GLfloat size){
	enable2DTexturing(id);
	glutSolidCube(size);
	disable2DTexturing();
}

// Draw a textured sphere
void TextureDrawer::drawTexturedSphere(int id, GLdouble radius, GLint slices, GLint stacks){
	enable2DTexturing(id);
	glutSolidSphere(radius, slices, stacks);
	disable2DTexturing();
}

// Draw a textured torus
void TextureDrawer::drawTexturedTorus(int id, GLdouble innerRadius, GLdouble outerRadius, GLint nsides, GLint rings){
	enable2DTexturing(id);
	glutSolidTorus(innerRadius, outerRadius, nsides, rings);
	disable2DTexturing();
}

// Draw a textured cone
void TextureDrawer::drawTexturedCone(int id, GLdouble base, GLdouble height, GLint slices, GLint stacks){
	enable2DTexturing(id);
	glutSolidCone(base, height, slices, stacks);
	disable2DTexturing();
}